import os
import sys

base_url = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(base_url)
sys.path.insert(0,base_url)

from db import db1


# res = db1.find("select * from dept")
# print(res)

from flask import current_app as app
class ACL:
    def __init__(self):
        pass
    # 给用户添加权限
    def set_resource(self,uid,relist):
        for i in relist:
            sql = "insert into user_resource(userid,rousourceid) values(%d,%d)"%(int(uid),int(i))
            db1.update(sql)
    # 修改权限
    def update_resource(self,uid,relist):
        # 先删除原来的再重新添加
        sql = "delete from user_resource where uid=%d"%(int(uid))
        db1.update(sql)
        self.set_resource(uid,relist)
    # 删除某个权限
    def delete_resource(self,uid,resourceid):
        pass
    # 获取页面资源列表
    def get_page_reslist(self,uid):
        sql = "select r.url from user_resource as ur inner join resource as r on ur.resourceid=r.id where r.type=1 and ur.userid=%d"%(int(uid))
        # print(sql)
        res = db1.find_all(sql)
        list = []
        for i in res:
            list.append(i['url'])
        return list
    # 获取接口资源
    def get_interface_relist(self,uid):
        sql = "select r.url from user_resource as ur inner join resource as r on ur.resourceid=r.id where r.type=2 and ur.userid=%d"%(int(uid))
        # print(sql)
        res = db1.find_all(sql)
        list = []
        for i in res:
            list.append(i['url'])
        return list
acl = ACL()
# acl.update_resource(1,2)